package com.example.springbootweb.zk;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;

/**
 * @Description: ZkConnection zk 连接客户端
 * @Author: mingtian
 * @CreateDate: 2019/9/20 16:07
 * @Version: 1.0
 */
public class ZkConnection {
    /**
     * 连接地址
     */
    public static final String IP_ADRESS = "127.0.0.1:2181";

    /**
     * 连接超时时间
     */
    public static final int TIMEOUT = 5000;

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException, NoSuchAlgorithmException {
        //1.建立 zk 连接
        ZooKeeper zk = new ZooKeeper(IP_ADRESS, TIMEOUT, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event.getState().name());
            }
        });

        //2. 创建持久节点 无序  OPEN_ACL_UNSAFE（完全开放权限）
        String path = zk.create("/eureka", "http://127.0.0.1:8080".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        //创建持久节点 有序   CREATOR_ALL_ACL（创建者所有权限）
//        String path =  zk.create("/eureka", "http://127.0.0.1:8080".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT_SEQUENTIAL);
//        //临时节点 无序       READ_ACL_UNSAFE（只读权限）
//        String path = zk.create("/eureka", "http://127.0.0.1:8080".getBytes(), ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.EPHEMERAL);

        System.out.println("path:" + path);

        //3.等待3秒 关闭连接
        Thread.sleep(3000);

        zk.close();


    }
}